package com.hlhj.funlike_business.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.SpecificationListAdp;
import com.hlhj.funlike_business.bean.FoodDetailBean;
import com.hlhj.funlike_business.presenter.MyCommodityDetailPresenter;
import com.hlhj.funlike_business.utils.WindowDialog;
import com.hlhj.funlike_business.views.MyCommodityDetailView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.loader.ImageLoaderInterface;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MyCommodityDetailAty extends BaseActivity<MyCommodityDetailView,MyCommodityDetailPresenter> implements MyCommodityDetailView {
    private TextView tv_editfood,tv_delfood,tv_foodTitle,tv_sale,tv_zan,tv_price,tv_content;
    private ImageView img_back;
    private Banner banner_mine_bg;
    private RecyclerView item_recy;
    private WindowDialog windowDialog;
    private SpecificationListAdp specificationListAdp;
    private List<FoodDetailBean.DataBean.AttrBean> attrBeans;

    private int id;
    @Override
    protected int getContentId() {
        return R.layout.aty_commoditydetail;
    }

    @Override
    protected void initData() {
        id = getIntent().getIntExtra("id",0);
        windowDialog=new WindowDialog(this);

        attrBeans=new ArrayList<>();
        item_recy.setLayoutManager(new GridLayoutManager(this,5));
        specificationListAdp=new SpecificationListAdp(attrBeans);
        item_recy.setAdapter(specificationListAdp);
    }

    @NotNull
    @Override
    public MyCommodityDetailView bindView() {
        return this;
    }

    @NotNull
    @Override
    public MyCommodityDetailPresenter createPresenter() {
        return new MyCommodityDetailPresenter();
    }

    @Override
    public void initView() {
        tv_editfood=findViewById(R.id.tv_editfood);
        tv_delfood=findViewById(R.id.tv_delfood);
        img_back=findViewById(R.id.img_back);
        banner_mine_bg=findViewById(R.id.banner_mine_bg);
        tv_foodTitle=findViewById(R.id.tv_foodTitle);
        tv_sale=findViewById(R.id.tv_sale);
        tv_zan=findViewById(R.id.tv_zan);
        tv_price=findViewById(R.id.tv_price);
        tv_content=findViewById(R.id.tv_content);
        item_recy=findViewById(R.id.item_recy);
    }

    @Override
    public void logicStart() {
        getPresenter().LoadFoodDetail(this,id);
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_editfood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyCommodityDetailAty.this, AddCommentyAty.class);
                intent.putExtra("id",id);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
        tv_delfood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });
    }

    public void showDialog(){
        windowDialog.show();
        TextView ok = windowDialog.findViewById(R.id.ok);
        TextView cancel = windowDialog.findViewById(R.id.cancel);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                windowDialog.dismiss();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPresenter().LoadFoodDel(MyCommodityDetailAty.this,id);
            }
        });
    }

    @Override
    public void showDetail(FoodDetailBean.DataBean dataBean) {
        tv_foodTitle.setText(dataBean.getGoods_name());
        tv_sale.setText("月售:" + dataBean.getSell_num());
        tv_zan.setText("赞:" + dataBean.getDiscount());
        tv_price.setText("￥" + dataBean.getThis_price());
        tv_content.setText(dataBean.getDetails());

        if (null!=dataBean.getPhotos()&& dataBean.getPhotos().size()>0){
            banner_mine_bg.setImages(dataBean.getPhotos());
            banner_mine_bg.setImageLoader(new GlideImageLoader());
            banner_mine_bg.start();
        }

        if (null!=dataBean.getAttr()&&dataBean.getAttr().size()>0){
            attrBeans.clear();
            attrBeans.addAll(dataBean.getAttr());
            specificationListAdp.notifyDataSetChanged();
        }
    }



    @Override
    public void showDelResult(String msg) {
        Toast.makeText(MyCommodityDetailAty.this,msg.toString(),Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post("刷新商品列表");
        finish();
    }

    private class GlideImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }
}
